//
//  myMoneyInfoViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/9/30.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "myMoneyInfoViewController.h"
#import "Masonry.h"
#import "CContactModel.h"
#import "CContactMgr.h"
#import "personMoneyInfoModel.h"
#import "myMoneyInfoTableViewCell.h"
#import "UIUtil.h"
#import "CXmppRequestMgr.h"
@interface myMoneyInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)CContactModel *currentUser;
@property(nonatomic,strong)NSMutableArray *infoArray;
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,assign)NSUInteger recordId;
@end

@implementation myMoneyInfoViewController
-(NSMutableArray *)infoArray
{
    if (_infoArray == nil) {
        _infoArray = [NSMutableArray array];
    }
    return _infoArray;
}
-(CContactModel *)currentUser
{
    if (_currentUser == nil) {
        _currentUser = [[[CContactMgr alloc]init] loadCurContact];
    }
    return _currentUser;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setNewTitleCenter:@"查看钱包明细"];
    self.view.backgroundColor = [UIColor whiteColor];
    [self configInterFace];
    __block myMoneyInfoViewController *weakSelf = self;
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
    }];
    [footer setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    self.tableview.footer = footer;
    self.recordId = 0;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    /*发送网络请求获取数据*/
    [self loadNewData];
}
-(void)configInterFace
{
    self.tableview = [[UITableView alloc]init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self.tableview registerNib:[UINib nibWithNibName:@"myMoneyInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"myMoneyInfoTableViewCell"];

    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableview) {
        return 60;
    }
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    myMoneyInfoTableViewCell *myMoneyInfoTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"myMoneyInfoTableViewCell" forIndexPath:indexPath];
    personMoneyInfoModel *model = self.infoArray[indexPath.row];
    myMoneyInfoTableViewCell .selectionStyle = UITableViewCellSelectionStyleNone;

    myMoneyInfoTableViewCell.infomodel = model;
    return myMoneyInfoTableViewCell;
}
-(void)loadNewData
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载数据...";
    __weak typeof (self) weakself = self;
    [[CXmppRequestMgr shareInstance] getMoneyListWithRecordId:self.recordId userId:self.currentUser.UserID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        [hud hide:YES];
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            NSArray *array = responseObject[@"record"];
            for (NSDictionary *dic in array) {
                [weakself.infoArray addObject:[weakself DictionaryToModelWithDictionary:dic]];
            }
            personMoneyInfoModel *model = weakself.infoArray.lastObject;
            weakself.recordId = model.recordid;
            [weakself.tableview reloadData];
        }
        else
        {
            [hud hide:YES];
            [MBProgressHUD showError:@"获取交易记录失败"];
        }
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [hud hide:YES];
        [MBProgressHUD showError:@"服务器异常"];

    }];
}
-(void)loadMoreData
{
    __weak typeof (self) weakself = self;
    [[CXmppRequestMgr shareInstance] getMoneyListWithRecordId:self.recordId userId:self.currentUser.UserID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
            NSArray *array = responseObject[@"record"];
            for (NSDictionary *dic in array) {
                [weakself.infoArray addObject:[weakself DictionaryToModelWithDictionary:dic]];
            }
            personMoneyInfoModel *model = weakself.infoArray.lastObject;
            weakself.recordId = model.recordid;
            [weakself.tableview.footer endRefreshing];
            [weakself.tableview reloadData];
        }
        else
        {
             [weakself.tableview.footer endRefreshing];
            [MBProgressHUD showError:@"获取交易记录失败"];
        }
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
         [weakself.tableview.footer endRefreshing];
        [MBProgressHUD showError:@"服务器异常"];
        
    }];

}
-(personMoneyInfoModel*)DictionaryToModelWithDictionary:(NSDictionary*)dic
{
    personMoneyInfoModel *model = [[personMoneyInfoModel alloc]init];
    NSString *type = dic[@"recordType"];
    switch (type.integerValue) {
        case 1:
            model.kind = redPacketOut;
            break;
        case 2:
            model.kind = redOacketIn;
            break;
        case 3:
            model.kind = moneyIn;
            break;
        case 4:
            model.kind = moneyOut;
            break;
        case 5:
            model.kind = returnMoney;
            break;
        default:
            break;
    }
    model.money = dic[@"amount"];
    model.time = dic[@"sendTime"];
    model.recordid = [dic[@"id"] integerValue];
    model.blance = dic[@"balance"];
    return model;
}
@end
